<?php

	Class Yomm_Devmod_Import {
		
		private $db = null;

		private $devmod_id = null;
		
		public $log = array();
		
		public $name = null;
		
		public function __construct($db_obj)
		{
			$this->db = $db_obj;
		}
		
		public function start($encoded_data)
		{
			$this->log(':: IMPORT START');
			
			//$this->log('Encoded Data: ' . $encoded_data);
			
			$data = base64_decode($encoded_data);
			
			//$this->log('Decoded Data: ' . $data);
			
			preg_match_all('/\<([a-zA-Z\_0-9]+)\>(.*)<\/([a-zA-Z\_0-9]+)\>/', $data, $result, PREG_SET_ORDER);
			
			if(count($result) == 0)
			{
				$this->log('error while importing devmod. nothing to foreach. $result is empty.');
				
				return false;
			}
			
			foreach($result as $tag)
			{

				$tag_content = base64_decode($tag[2]);
				
				switch($tag[1])
				{
					default :
						$this->log('error while importing devmod');
						return false;
					break;
					
					case "TABLE_STRUCT" :
						$this->log('table structure info: '.$tag_content);
						$ret = $this->db->query($tag_content);
					
						$this->log('table structure generated; return = '. $ret);
					break;
					
					case "TABLE_DATA" :
						$this->log('table data info: '.$tag_content);
						if(is_null($tag_content) || empty($tag_content))
						{
							$this->log('table data is_null; no insertion!');
							continue; break;	
						}
						$ret = $this->db->query($tag_content);
						
						$this->log('table data added into database; return = '. $ret);
					break;
					
					case "DEVMOD_SQL" :
						
						$this->log('dev_mod entry start');
						
						$arr = unserialize($tag_content);
						
						// check if table already exists
						$dev_sql = $this->db->select("dev_sql", "id", "WHERE name='{$arr['sql_tablename']}'", false);
						
						$this->log($this->db->last_query .'; sql_numrows = ' . count($dev_sql));
						
						if(count($dev_sql) > 1) {
							$err = new Error('error while import routine: the dev_sql count is bigger then 1! something is wired. check table dev_Sql');
							$this->log('ERROR: dev_sql num rows is highter then 1. where clausle: name=' . $arr['sql_tablename']);
							echo $err->getMessage(); exit;
						} elseif(count($dev_sql) == 1) {
							$dev_sql_id = $dev_sql->id;
							$this->log('sql_numrows is 1; name=' . $arr['sql_tablename']);
						} else {
							$dev_sql_id = $this->db->handler("dev_sql", array("name" => $arr['sql_tablename']), false);
							$this->log('sql_numrows is 0; dev_mod entry was created now on id: '.$dev_sql_id.'; name=' . $arr['sql_tablename']);
						}
						
						// check if handler entry exists
						$dev_mod = $this->db->select("dev_mods", "id", "WHERE name='{$arr['name']}'", false);
						
						$this->log($this->db->last_query .'; sql_numrows = ' . count($dev_mod));
						
						if(count($dev_mod) > 1) {
							$err = new Error('error while import routine: the dev_mods count is bigger then 1! something is wired. check table dev_mods');
							$this->log('ERROR: dev_mod num rows is highter then 1. where clausle: name=' . $arr['name']);
							echo $err->getMessage(); exit;
						} elseif (count($dev_mod) == 1) {
							$dev_mod_add = $this->db->handler("dev_mods", array(
								"code" => addslashes(base64_decode($arr['code']))
							), "WHERE id='{$dev_mod->id}'");
							
							$devmod_id_tmp = $dev_mod->id;
							
							$this->log('sql_numrows is 1; JUST UPDATE THE CODE; name=' . $arr['name']);
						} else {
							$dev_mod_add = $this->db->handler("dev_mods", array(
								"dev_sql_id" => $dev_sql_id,
								"name" => $arr['name'],
								"code" => addslashes(base64_decode($arr['code'])),
								"theme" => "{$arr['theme']}",
								"tpl_frontend_list" => $arr['tpl_frontend_list'],
								"tpl_frontend_add" => $arr['tpl_frontend_list']
							), false);
							
							$devmod_id_tmp = $dev_mod_add;
							
							$this->log('sql_numrows is 0; ADD new Column and create; created id = '.$dev_mod_add.'; name=' . $arr['name']);
						}
						
						$this->log('INSERTED/UPDATED CODE: ' . base64_decode($arr['code']));
						
						$this->log('start write the new internal navigation');
						
						$row = $this->db->select("admin_nav", "title", "WHERE title='{$arr['name']}'", true);
						
						$this->log($this->db->last_query.'; sql_numrows = ' . count($row));
						
						if(count($row) == 0)
						{
							$this->log('now start to write entry for devmod id: ' . $devmod_id_tmp);
							$insert_admin_nav = $this->db->handler("admin_nav", array(
								'internal_method' => 'devmod_show',
								'title' => $arr['name'],
								'block_id' => 4,
								'extras' => 'id='.$devmod_id_tmp
							));	
							$this->log($this->db->last_query.'; result id: '. $insert_admin_nav);
						} else {
							$this->log('the admin_nav entry is already exisiting. do not insert anything');
						}
						
						$this->log('dev_mod entry end');
						
					break;
				}
			}
			
			$this->log(':: IMPORT END');
			
			return true;
		}
		
		private function log($text)
		{
			$this->log[] = array(
				'date' => date("H:i:s"),
				'text' => $text
			);
		}
		
		public function getLog()
		{
			$str = '<table border="1" class="color_table">';
			
			foreach($this->log as $item)
			{
				$str .= "<tr><td>" . $item['date'] . "</td><td>" . $item['text'] . "</td></tr>";
			}
			
			return $str . "</table>";
		}
	}